package com.ftg.learn.bus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.VO.busUseRegidtrationVo;
import com.ftg.learn.bus.entity.BusUseRegistration;
import com.ftg.learn.bus.mapper.BusUseRegistrationMapper;
import com.ftg.learn.bus.service.IBusUseRegistrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公车使用登记表 服务实现类
 * </p>
 *
 * @author LSY
 * @since 2022-03-10
 */
@Service
public class BusUseRegistrationServiceImpl extends ServiceImpl<BusUseRegistrationMapper, BusUseRegistration> implements IBusUseRegistrationService {

    @Resource
    BusUseRegistrationMapper busUseRegistrationMapper;

    /**
     *添加公车使用登记表所有信息
     * @param user_name
     * @param user_phone
     * @param start_place
     * @param end_place
     * @param end_mileage
     * @param is_special
     * @param actual_purpose
     * @return
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean insertInfor(Long bid, String user_name,String user_phone,
                               String start_place,String end_place,String end_mileage,
                               String return_time,String is_special,String actual_purpose) {
        Map<String,Object> map = busUseRegistrationMapper.selectAutoInfor(bid);
        Long a = Long.valueOf(String.valueOf(map.get("bid")));
        String b = String.valueOf(map.get("dname"));
        String c = String.valueOf(map.get("dphone"));
        String d = String.valueOf(map.get("receiver"));
        //转换前台添加使用情况文字为0或1
        String shi = "有";
        String fou = "无";
        if(is_special.equals(fou)){
            is_special = "0";
        }else if (is_special.equals(shi)){
            is_special = "1";
        }
        String start_mileage = "0";
        List<Map<String,Object>> busUseList = busUseRegistrationMapper.selectAutoInforoldend_place(bid);
        if (busUseList.size() != 0){
            Map<String, Object> mapold = busUseList.get(busUseList.size() - 1);
            if(mapold.get("bid") != null){
                //获取数据库中当前的end_mileage
                String oldend_mileage = String.valueOf(mapold.get("end_mileage"));
                start_mileage = oldend_mileage;
                //获取当前时间赋予为当前return_time的默认值
                Date time = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return_time = (sdf.format(time));
            }
        }
        busUseRegistrationMapper.insertAutoInfor(a,b,c,d,user_name,user_phone,start_place,end_place
                ,start_mileage,end_mileage,return_time,is_special,actual_purpose);
        return true;
    }

    /**
     * 按照使用表id查询
     * @param burid
     * @return
     */
    @Override
    public List<BusUseRegistration> selectById1(Long burid){
        return busUseRegistrationMapper.selectById1(burid);
    }

    /**
     * 按名称查询
     * @param bus_name
     * @return
     */
    @Override
    public List<BusUseRegistration> busShowByName(String bus_name){
        return busUseRegistrationMapper.busShowByName(bus_name);
    }

    /**
     * 分页查询
     * @param pagination
     * @return
     */
    @Override
    public List<BusUseRegistration> showBusName(Page<BusUseRegistration> pagination,String bus_name){
        return busUseRegistrationMapper.showBusName(pagination,bus_name);
    }

    /**
     * 分页查询
     * @param pagination
     * @return
     */
    @Override
    public List<busUseRegidtrationVo> showBusNamePage(Page<busUseRegidtrationVo> pagination){
        return busUseRegistrationMapper.showBusNamePage(pagination);
    }

    /**
     * 查询公车表里的bid,bus_name
     * @return
     */
    @Override
    public List<Map<String,String>> showAllName(){
        return busUseRegistrationMapper.showAllName();
    }
}
